<?php
class sign extends base{

	public function init(){ // 公用
		parent::init(); // 这是必须的

		$this->tit = "签到";
		$this->current = 'my';

		$this->modelObj = D('company_admin');
		$this->sign_logObj = D('company_sign_log');

		$this->checkLogin();
	}


	public function index()
	{
		if($this->fmdo == 'sign'){

			$sign = arg('sign');
			//权限
			switch($sign){
				case '0':
					$msg ="下班签到成功";
					break;
				case '1':
					$msg ="上班签到成功";
					break;
			}
			$arrBase = array(
				'sign' =>$sign,
			);
			$this->modelObj->update(array('id'=>$this->admin_id), $arrBase);

			//日志
			$arrBase = array(
				'company_id'=>$this->company_id,
				'admin_id'=>$this->admin_id,
				'msg'=>$msg,
				'sign_status' =>$sign,
				'add_time' =>date('Y-m-d H:i:s'),
				'status' =>1,
			);
			$this->sign_logObj->create($arrBase);

			$this->renderSuccess($msg);
		}

		if($this->fmdo == 'zhiban'){

			$zhiban = arg('zhiban');
			//权限
			switch($zhiban){
				case '0':
					$msg ="取消值班成功";
					break;
				case '2':
					$msg ="值班成功";
					break;
			}
			$arrBase = array(
				'zhiban' =>$zhiban,
			);
			$this->modelObj->update(array('id'=>$this->admin_id), $arrBase);

			//日志
			$arrBase = array(
				'company_id'=>$this->company_id,
				'admin_id'=>$this->admin_id,
				'msg'=>$msg,
				'sign_status' =>$zhiban,
				'add_time' =>date('Y-m-d H:i:s'),
				'status' =>1,
			);
			$this->sign_logObj->create($arrBase);

			$this->renderSuccess($msg);
		}

	}


	//loaddata
	public function loaddata(){

		$cond = "status=1 ".$this->cond_base;
		// 加入查询条件
		if($this->tab){
			switch($this->tab){
				case '8':
					$cond .=" and order_status=99";
					break;
				default:
					$cond .=" and order_status={$this->tab}";
			}
		}
		//权限
		switch($this->admin['level']){
			case '0'://管理员
				$cond .=" ";
				break;
			case '1'://报修员
				$cond .=" and admin_id={$this->admin_id}";
				break;
			case '2'://报修组长
				$cond .=" ".$this->cond_cate.$this->cond_zone;
				break;
			case '3'://维修师傅
				$cond .=" and shifu_id={$this->admin_id}";
				break;
			case '4'://维修组长
				$cond .=" ".$this->cond_cate.$this->cond_zone;
				break;
		}

		//搜索
		if($this->keyword){
			$cond.=" and (title like '%".$this->keyword."%' or intro like '%".$this->keyword."%' and local like '%".$this->keyword."%') ";
		}

		$result = $this->orderObj->getList($cond);
        if($result){
			$lists = $result['list'];

			//
        	// foreach ($lists as $val) {
			// 	if($val['photo']){
			// 		$photos = explode(',', $val['photo']);
			// 		$val['img'] = $photos[0];
			// 	}
			// 	$list[] = $val;
			// }
        	// $this->list = $list;
			// $this->showPage = $result['showPage'];
			// $this->total = $result['total'];

			$this->list = $lists;

			$this->areas = $this->areaObj->ids($lists,'area_id');
			$this->cates = $this->cateObj->ids($lists,'cate_id');
			$this->zones = $this->zoneObj->ids($lists,'zone_id');
			$this->devices = $this->deviceObj->ids($lists,'device_id');
			$this->admins = $this->adminObj->ids($lists,'admin_id');
			$this->shifus = $this->adminObj->ids($lists,'shifu_id');
		}else{
			die('0');
		}

	}


	function log(){

		$arrBase = array('tab' => $this->tab,'page' => '0000',);
		if($this->keyword){
			$arrBase = array_merge($arrBase,array('keyword' => $this->keyword));
		}
		$this->nextpage = url('sign','loaddata', $arrBase);

	}

	//loaddata
	function loaddata(){

		$cond = "status=1 and admin_id={$this->admin_id} and ".$this->cond;

		//搜索
		if($this->keyword){
			$cond.=" and (msg like '%".$this->keyword."%' ) ";
		}

		$page = (int)arg("page", 1);
		$result = $this->sign_logObj->findAll(array($cond),'id desc','*',array($page, 15));
		$this->pager = $this->sign_logObj->page;
        if($this->pager){  //2页的意思
			if($this->pager['total_page'] < arg('page') ) {
	            die('0');
	        }
		}else{
			if(arg('page')>1) {
	            die('0');
	        }
		}
		if($result){
			$this->list = $result;
		}else{
			die('0');
		}


	}

}